package com.cakin.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import static com.cakin.constant.Constant.USER_SESSION;

/**
 * @ClassName: LoginServlet
 * @Description: 登录Servlet
 * @Date: 2020/6/16
 * @Author: cakin
 */
public class LoginServlet extends HttpServlet {
    /**
     * 功能描述：用户登录逻辑
     *
     * @author cakin
     * @date 2020/6/16
     * @param req 请求
     * @param resp 响应
     * @throws IOException IOException
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String username = req.getParameter("username");

        if (username.equals("admin")) {
            req.getSession().setAttribute(USER_SESSION, req.getSession().getId());
            // 登录成功登录到首页
            resp.sendRedirect("/sys/home.jsp");
        } else {
            // 登录失败重定向到错误页面
            resp.sendRedirect("/error.jsp");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
